﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;

using EntitySpaces.Interfaces;
using yncTech.BusinessObjects;
using yncTech.Common;

public partial class Admin_Fin_fin003d : AdminBasePage
{
    decimal shipID = -1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[FinShippingMetadata.ColumnNames.ShipID] != null)
        {
            shipID = decimal.Parse(Request.QueryString[FinShippingMetadata.ColumnNames.ShipID]);
        }

        if (!IsPostBack)
        {
            FinShipping fs = new FinShipping();
            fs.LoadByPrimaryKey(shipID);

            txtFile.Text = fs.AttachmentName;
        }

    }

    protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
    {
        if (IsPostBack)
        {
            return;
        }
        GridDataBind(e.NewPageIndex);
    }

    void GridDataBind(int page)
    {

        VBoxReceiptsQuery q1 = new VBoxReceiptsQuery();
        FinShippingBoxesQuery q2 = new FinShippingBoxesQuery();

        q1.SelectAll();
        q1.InnerJoin(q2).On(q1.BoxID == q2.BoxID);
        q1.Where(q2.ShipID == shipID);

        pageGrid.DataSource = q1.LoadDataTable();
        pageGrid.CurrentPage = page;
        pageGrid.DataBind();
    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string fileName = string.Empty;
        lbMessage.Text = "";
        if (fileUpload.HasFile)
        {
            try
            {
                fileName = Guid.NewGuid().ToString() + Path.GetExtension(fileUpload.FileName);
                string filePath = Server.MapPath("/UploadFile/") + fileName;
                fileUpload.SaveAs(filePath);
                txtFile.Text = fileName;
            }
            catch (Exception ex)
            {
                lbMessage.Text = ex.Message;
            }
        }
        else
        {
            lbMessage.Text = "请选择要上传的文件!";
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        lbMessage.Text = "";
        if (string.IsNullOrEmpty(txtFile.Text))
        {
            lbMessage.Text = "请上传相关附件后再确认!";
            return;
        }
        FinShipping fs = new FinShipping();
        fs.LoadByPrimaryKey(shipID);
        fs.ShippingStatus = ConstantValue.ShippingStatusCode.Waiting;
        fs.Sender = LoginUserInfo.CurrentUser.UserName;
        fs.ShippingDate = DateTime.Now;
        fs.UpdatedBy = LoginUserInfo.CurrentUser.LoginName;
        fs.AttachmentName = txtFile.Text;
        fs.UpdatedDttm = DateTime.Now;
        fs.Save();

        FinShippingBoxesCollection shippingBoxes = new FinShippingBoxesCollection();
        shippingBoxes.Query.Where(shippingBoxes.Query.ShipID == shipID);
        shippingBoxes.Query.Load();

        var boxIds = shippingBoxes.Select(r => r.BoxID).ToArray();

        FinBoxCollection boxes = new FinBoxCollection();
        boxes.Query.Where(boxes.Query.BoxID.In(boxIds));
        boxes.Query.Load();

        foreach (var item in boxes)
        {
            item.StorageStatus = ConstantValue.StorageStatusCode.Transfer;
            item.UpdatedBy = LoginUserInfo.CurrentUser.UserName;
            item.UpdatedDttm = DateTime.Now;
        }

        boxes.Save();

        WebHelper.CloseDialog(true);


    }
}